package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.ProductModel;
import com.zeng.jiudian.dao.ProductDao;
import com.zeng.jiudian.dao.impl.ProductDaoImpl;
import com.zeng.jiudian.service.ProductService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/23 - 15:46
 */
public class ProductServiceImpl implements ProductService
{
    @Override
    public int add(ProductModel productModel)
    {
        ProductDao productDao = new ProductDaoImpl();

        int count = productDao.add(productModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel)
    {
        ProductDao productDao = new ProductDaoImpl();

        return productDao.findAll(productModel);
    }

    @Override
    public int delById(int id)
    {
        ProductDao productDao = new ProductDaoImpl();

        int count = productDao.delById(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public ProductModel findById(int id)
    {
        ProductDao productDao = new ProductDaoImpl();

        return productDao.findById(id);
    }

    @Override
    public int update(ProductModel productModel)
    {
        ProductDao productDao = new ProductDaoImpl();

        if ("".equals(productModel.getProductName()))
        {
            productModel.setProductName(null);
        }
        if ("".equals(productModel.getEnName()))
        {
            productModel.setEnName(null);
        }
        if (productModel.getMarketPrice() < 0)
        {
            productModel.setMarketPrice(null);
        }
        if (productModel.getPrice() < 0)
        {
            productModel.setPrice(null);
        }
        if ("".equals(productModel.getContent()))
        {
            productModel.setContent(null);
        }
        if (productModel.getIsShow() < 0)
        {
            productModel.setIsShow(null);
        }
        if (productModel.getRecommend() < 0)
        {
            productModel.setRecommend(null);
        }
        if ("".equals(productModel.getImg()))
        {
            productModel.setImg(null);
        }
        if ("".equals(productModel.getIntroduction()))
        {
            productModel.setIntroduction(null);
        }

        int count = productDao.update(productModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public int getCount(ProductModel productModel)
    {
        ProductDao productDao = new ProductDaoImpl();

        return productDao.getCount(productModel);
    }

    @Override
    public List<ProductModel> getAll()
    {
        List<ProductModel> list = null;
        Jedis jedis = new Jedis();
        jedis.select(0);
        Long length = jedis.llen("productId");
        if (length > 0)
        {
            list = new ArrayList<>();
            List<String> productIds = jedis.lrange("productId", 0, length);
            List<String> productName = jedis.lrange("productProductName", 0, length);
            List<String> enName = jedis.lrange("productEnName", 0, length);
            List<String> marketPrice = jedis.lrange("productMarketPrice", 0, length);
            List<String> price = jedis.lrange("productPrice", 0, length);
            List<String> content = jedis.lrange("productContent", 0, length);
            List<String> introduction = jedis.lrange("productIntroduction", 0, length);
            List<String> isShow = jedis.lrange("productIsShow", 0, length);
            List<String> recommend = jedis.lrange("productRecommend", 0, length);
            List<String> img = jedis.lrange("productImg", 0, length);
            List<String> createTime = jedis.lrange("productCreateTime", 0, length);
            List<String> updateTime = jedis.lrange("productUpdateTime", 0, length);
            for (int i = 0; i < length; i++)
            {
                ProductModel model = new ProductModel();
                model.setId(Integer.parseInt(productIds.get(i)));
                model.setProductName(productName.get(i));
                model.setEnName(enName.get(i));
                model.setMarketPrice(Double.parseDouble(marketPrice.get(i)));
                model.setPrice(Double.parseDouble(price.get(i)));
                model.setContent(content.get(i));
                model.setIntroduction(introduction.get(i));
                model.setIsShow(Integer.parseInt(isShow.get(i)));
                model.setRecommend(Integer.parseInt(recommend.get(i)));
                model.setImg(img.get(i));
                model.setCreateTime(createTime.get(i));
                model.setUpdateTime(updateTime.get(i));
                list.add(model);
            }
        }
        else
        {
            ProductDao productDao = new ProductDaoImpl();
            list = productDao.getAll();
            jedis.del("productId", "productProductName", "productEnName", "productMarketPrice", "productPrice", "productContent", "productIntroduction", "productIsShow", "productRecommend", "productImg", "productCreateTime", "productUpdateTime");
            for (ProductModel model : list)
            {
                jedis.rpush("productId", model.getId().toString());
                jedis.rpush("productProductName", model.getProductName());
                jedis.rpush("productEnName", model.getEnName());
                jedis.rpush("productMarketPrice", model.getMarketPrice().toString());
                jedis.rpush("productPrice", model.getPrice().toString());
                jedis.rpush("productContent", model.getContent());
                jedis.rpush("productIntroduction", model.getIntroduction());
                jedis.rpush("productIsShow", model.getIsShow().toString());
                jedis.rpush("productRecommend", model.getRecommend().toString());
                jedis.rpush("productImg", model.getImg());
                jedis.rpush("productCreateTime", model.getCreateTime());
                jedis.rpush("productUpdateTime", model.getUpdateTime());
            }
        }


        return list;
    }
}
